<template>
  <div>
    <div class="filter-container">
      <div class="filter-item">
        <span>分组名称或编码:</span>
        <el-input v-model="listQuery.keyword" clearable placeholder="请输入分组名称或编码" @keyup.enter="handleFilter" />
      </div>
      <el-button v-permission="['sys:dict:group:list']" class="filter-item" type="primary" icon="Search" @click="handleFilter">搜索</el-button>

      <el-button class="filter-item" text type="primary" icon="Delete" @click="clearQuery">清除条件</el-button>
    </div>
    <div class="operationList">
      <div>
        <el-button v-permission="['sys:dict:group:add']" text type="primary" icon="Plus" @click="handleCreate">新增</el-button>
      </div>
      <div>
        <el-button text type="primary" icon="RefreshRight" @click="handleFilter">刷新</el-button>
      </div>
    </div>
    <el-table v-loading="listLoading" :row-class-name="tableRowClass" :data="list" fit border highlight-current-row header-cell-class-name="header-cell" @row-click="refreshDicts">
      <el-table-column label="序号" width="100" type="index" />
      <el-table-column prop="name" label="分组名称" width="160" />
      <el-table-column prop="code" label="分组编码" width="160" />
      <el-table-column prop="remarks" label="备注" width="160" />
      <el-table-column label="操作" width="180">
        <template #default="scope">
          <el-button v-permission="['sys:dict:group:update']" size="small" plain type="primary" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button v-permission="['sys:dict:group:delete']" size="small" plain type="danger" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" v-model:page="listQuery.page" v-model:limit="listQuery.limit" :total="total" :page-sizes="pageArray" @pagination="getList" />

    <dictGroupForm ref="dictGroupForm" @refresh-list="refreshDict"/>
  </div>
</template>

<script>
import { fetchDictGroupList, createDictGroup, deleteDictGroup, updateDictGroup } from '@/api/sys/dict/dictGroup'
import Pagination from '@/components/Pagination/index.vue'
import dictGroupForm from './dictGroupForm.vue'
export default {
  name: 'SysDictGroupComponent',
  components: { Pagination, dictGroupForm },
  emits: ['refreshDicts'],
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      pageArray: this.$store.dictStore.pageArray,
      listQuery: {
        page: 1,
        limit: this.$store.dictStore.defaultPageSize,
        importance: undefined,
        title: undefined,
        type: undefined
      },
      showReviewer: false

    }
  },
  created() {
    this.getList()
  },
  methods: {
    /**
     * 刷新字典列表
     * @param row 选中数据
     */
    refreshDict() {
      this.getList()
    },
    /**
     * 获取字典列表
     */
    getList(loading = true) {
      this.listLoading = loading
      fetchDictGroupList(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    /**
     * 刷新字典列表
     * @param row 选中数据
     */
    refreshDicts(row) {
      this.$emit('refreshDicts', row)
    },
    /**
     * 搜索
     */
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    /**
     * 修改状态
     * @param row 选中数据
     * @param status 状态
     */
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },

    /**
     * 新增
     */
    handleCreate() {
      this.$refs.dictGroupForm.handleCreate()
    },
    /**
     *提交新增
     */
    createData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          createDictGroup(this.temp).then((response) => {
            this.loading = false
            const data = response.data
            if (data.success) {
              this.$store.dictStore.initDict()
              this.dialogFormVisible = false
              this.$message.success('创建成功')
              this.getList()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    /**
     * 编辑
     * @param row 选中数据
     */
    handleUpdate(row) {
      this.$refs.dictGroupForm.handleUpdate(row)
    },
    /**
     * 提交编辑
     */
    updateData() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          this.loading = true
          const tempData = Object.assign({}, this.temp)
          updateDictGroup(tempData).then((response) => {
            this.loading = false
            const data = response.data
            if (data.success) {
              this.$store.dictStore.initDict()
              this.dialogFormVisible = false
              this.$message.success(data.msg)
              this.getList()
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    /**
     * 删除
     * @param row 选中数据
     */
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        row.$delAnim = true
        deleteDictGroup([row.id]).then((response) => {
          const data = response.data
          if (data.success) {
            row.$delAnim = true
            this.$store.dictStore.initDict()
            this.dialogFormVisible = false
            this.$message.success('删除成功')
            this.getList(false)
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    }
  }
}
</script>

